package cn.edu.zut.salarymanagesystem.activities;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import cn.edu.zut.salarymanagesystem.R;
import cn.edu.zut.salarymanagesystem.config.Storage;
import cn.edu.zut.salarymanagesystem.fragments.AddFragment;


public class AddSpecialActivity extends AppCompatActivity implements View.OnClickListener {


    private TextView group_name;
    private TextView username;
    private TextView tv_commentary;
    private EditText edit_special_commentary;
    private EditText edit_special_price;
    private Button add;
    private final static byte SUCCESS = 0x01;
    private final static byte BAD_FORMAT = 0x02;
    Handler handler = new Handler() {
        @Override
        public void handleMessage(@NonNull Message msg) {
            switch (msg.what) {
                case BAD_FORMAT: {
                    Toast.makeText(getApplicationContext(), "请输入完整您要添加的数据！", Toast.LENGTH_SHORT).show();
                    break;
                }
            }
        }
    };

    @SuppressLint("MissingInflatedId")
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_add_special);
        //要直接用绑定好的控件.setText()设置到页面中
        group_name = findViewById(R.id.group_name);
        group_name.setText("您在 " + Storage.currentUser.getGroupName());
        username = findViewById(R.id.username);
        username.setText("欢迎您 " + Storage.currentUser.getUserName());
        tv_commentary = findViewById(R.id.tv_message);
        edit_special_commentary = findViewById(R.id.edit_special_commentary);
        edit_special_price = findViewById(R.id.edit_special_price);
        add = findViewById(R.id.btn_add_special);
        findViewById(R.id.btn_back).setOnClickListener(v -> this.finish());
        add.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        int result_code = 100;
        Intent intent = new Intent(AddSpecialActivity.this, AddFragment.class);
        if (edit_special_commentary.getText() == null ||
                edit_special_commentary.getText().toString().equals("") ||
                edit_special_price.getText() == null ||
                edit_special_price.getText().toString().equals("")) {
            handler.sendEmptyMessage(BAD_FORMAT);
            return;
        }
        Bundle bundle = new Bundle();
        bundle.putString("commentary", edit_special_commentary.getText().toString());
        bundle.putString("price", edit_special_price.getText().toString());
        intent.putExtras(bundle);
        setResult(result_code, intent);
        finish();
    }
}
